package com.yunxie.novelservice.application.controller;


import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.yunxie.novelservice.application.convert.NovelDetailsDTOConverter;
import com.yunxie.novelservice.application.dto.NovelDetailsDTO;
import com.yunxie.novelservice.common.entity.Result;
import com.yunxie.novelservice.domain.entity.NovelDetailsBo;
import com.yunxie.novelservice.domain.service.NovelDetailsDomainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 小说详情模块
 */
@RestController
@RequestMapping("/novel/details")
@Slf4j
public class NovelDetailsController {
    @Resource
    private NovelDetailsDomainService novelDetailsDomainService;

    /**
     * 查询小说详情接口
     * @param novelDetailsDTO
     * @return
     */
    @PostMapping("queryNovelInfo")
    public Result<NovelDetailsDTO> queryNovelInfo(@RequestBody NovelDetailsDTO novelDetailsDTO){
        try{
            if(log.isInfoEnabled()){
                log.info("NovelDetailsController.queryNovelInfo.dto:{}", JSON.toJSONString(novelDetailsDTO));
            }
            Preconditions.checkNotNull(novelDetailsDTO.getDetailId(),"小说id不能为空");
            NovelDetailsBo novelDetailsBo = NovelDetailsDTOConverter.INSTANCE.convertDtoToBo(novelDetailsDTO);
            NovelDetailsBo queryNovelInfo = novelDetailsDomainService.queryNovelInfo(novelDetailsBo.getDetailId());
            NovelDetailsDTO result = NovelDetailsDTOConverter.INSTANCE.convertBoToDto(queryNovelInfo);

            return  Result.ok(result);
        }catch (Exception e){
            log.error("NovelDetailsController.queryNovelInfo.error:{}",e.getMessage(),e);
            return Result.fail("获取小说详情失败");
        }
    }
    /**
     * 删除小说详情
     */
    @PostMapping("DeleteNovelInfo")
    public Result<Boolean> DeleteNovelInfo(@RequestBody NovelDetailsDTO novelDetailsDTO){
        try{
            if(log.isInfoEnabled()){
                log.info("NovelDetailsController.queryNovelInfo.dto:{}", JSON.toJSONString(novelDetailsDTO));
            }
            Preconditions.checkNotNull(novelDetailsDTO.getDetailId(),"小说id不能为空");
            NovelDetailsBo novelDetailsBo = NovelDetailsDTOConverter.INSTANCE.convertDtoToBo(novelDetailsDTO);
            novelDetailsDomainService.DeleteNovelInfo(novelDetailsBo);
            return  Result.ok(true);
        }catch (Exception e){
            log.error("NovelDetailsController.queryNovelInfo.error:{}",e.getMessage(),e);
            return Result.fail("删除小说详情失败");
        }
    }
    /**
     * 增加小说详情
     */
    @PostMapping("addNovelInfo")
    public Result<Boolean> addNovelInfo(@RequestBody NovelDetailsDTO novelDetailsDTO){
        try{
            if(log.isInfoEnabled()){
                log.info("NovelDetailsController.addNovelInfo.dto:{}", JSON.toJSONString(novelDetailsDTO));
            }
           // Preconditions.checkNotNull(novelDetailsDTO.getDetailId(),"小说id不能为空");
            Preconditions.checkNotNull(novelDetailsDTO.getWordCount(),"小说字数不能为空");
            Preconditions.checkNotNull(novelDetailsDTO.getAuthor(),"小说作者不能为空");
            Preconditions.checkNotNull(novelDetailsDTO.getNovelsId(),"小说名字不能为空");
            Preconditions.checkNotNull(novelDetailsDTO.getReadCount(),"阅读次数不能为空");
            Preconditions.checkNotNull(novelDetailsDTO.getDescription(),"小说介绍不能为空");
            Preconditions.checkNotNull(novelDetailsDTO.getNovelsId(),"小说类型不能为空");
            NovelDetailsBo novelDetailsBo = NovelDetailsDTOConverter.INSTANCE.convertDtoToBo(novelDetailsDTO);
            novelDetailsDomainService.addNovelInfo(novelDetailsBo);
            return  Result.ok(true);
        }catch (Exception e){
            log.error("NovelDetailsController.addNovelInfo.error:{}",e.getMessage(),e);
            return Result.fail("添加小说详情失败");
        }
    }
    /**
     * 增加小说详情
     */
    @PostMapping("updateNovelInfo")
    public Result<Boolean> updateNovelInfo(@RequestBody NovelDetailsDTO novelDetailsDTO){
        try{
            if(log.isInfoEnabled()){
                log.info("NovelDetailsController.updateNovelInfo.dto:{}", JSON.toJSONString(novelDetailsDTO));
            }
             Preconditions.checkNotNull(novelDetailsDTO.getDetailId(),"小说id不能为空");
            Preconditions.checkNotNull(novelDetailsDTO.getWordCount(),"小说字数不能为空");
            Preconditions.checkNotNull(novelDetailsDTO.getAuthor(),"小说作者不能为空");
            Preconditions.checkNotNull(novelDetailsDTO.getNovelsId(),"小说名字不能为空");
            Preconditions.checkNotNull(novelDetailsDTO.getReadCount(),"阅读次数不能为空");
            Preconditions.checkNotNull(novelDetailsDTO.getDescription(),"小说介绍不能为空");
            Preconditions.checkNotNull(novelDetailsDTO.getNovelsId(),"小说类型不能为空");
            NovelDetailsBo novelDetailsBo = NovelDetailsDTOConverter.INSTANCE.convertDtoToBo(novelDetailsDTO);
            novelDetailsDomainService.updateNovelInfo(novelDetailsBo);
            return  Result.ok(true);
        }catch (Exception e){
            log.error("NovelDetailsController.updateNovelInfo.error:{}",e.getMessage(),e);
            return Result.fail("修改小说详情失败");
        }
    }

}
